package com.googlecode.delivery.config.impl;

import java.io.File;
import java.io.IOException;

import javax.swing.filechooser.FileSystemView;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import com.googlecode.delivery.config.IConfigurationCmd;

import com.googlecode.delivery.DeliveryException;
/**
 * 
 * @author Stephen Kay
 */
public class ConfigurationCmd implements IConfigurationCmd {

	private Options options;

	/**
	 * 
	 */
	public ConfigurationCmd() {
		this.options = createOptions();
	}

	/**
	 * @return
	 */
	@SuppressWarnings("static-access")
	static Options createOptions() {
		Options options = new Options();
		Option artefact = OptionBuilder.withArgName("file").hasArg()
				.withDescription("use given buildfile").withLongOpt("artefact")
				.isRequired().create("a");
		options.addOption(artefact);
		Option env = OptionBuilder.withArgName("file").hasArg()
				.withDescription("use given buildfile").withLongOpt("env")
				.isRequired().create("e");

		options.addOption(env);
		Option buildfile = OptionBuilder.withArgName("file").hasArg()
				.withDescription("use given buildfile")
				.withLongOpt("outputdir").create("o");

		options.addOption(buildfile);
		return options;
	}

	private File artefact;
	private File environment;
	private File outputDir;

	/*
	 * (non-Javadoc)
	 * 
	 * @see ch.sword.toolkit.config.IConfigurationCmd#getArtefact()
	 */
	public File getArtefact() {
		return artefact;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ch.sword.toolkit.config.IConfigurationCmd#getEnvironment()
	 */
	public File getEnvironment() {
		return environment;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ch.sword.toolkit.config.IConfigurationCmd#getOutputDir()
	 */
	public File getOutputDir() {
		return outputDir;
	}

	/**
	 * @param options
	 * @param args
	 * @return
	 * @throws DeliveryException
	 */
	static CommandLine createCommandLine(Options options, String[] args)
			throws DeliveryException {
		try {
			CommandLineParser parser = new GnuParser();
			// parse the command line arguments
			CommandLine commandLine = parser.parse(options, args);
			return commandLine;
		} catch (ParseException exp) {
			// oops, something went wrong
			String msg = "Parsing error - Reason: " + exp.getMessage();
			throw new DeliveryException(msg, exp);
		}

	}

	static File getFile(CommandLine line, String option) {
		String path = line.getOptionValue(option);
		File file = null;
		if (path != null) {
			file = new File(path.replace('\'', ' ').trim());
		}
		return file;
	}

	public void parse(String[] args) throws IOException, DeliveryException {
		CommandLine line = createCommandLine(options, args);
		this.environment = getFile(line, "e");
		this.artefact = getFile(line, "a");
		this.outputDir = getFile(line, "o");
		if(outputDir==null){
			outputDir = FileSystemView.getFileSystemView().createFileObject(".");
		}
	}

	public void printHelp() {
		HelpFormatter formatter = new HelpFormatter();
		formatter.printHelp("configure", options);
	}

}
